﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Media.Imaging;

namespace RobotEscape.Screens
{
    public class HelpScreen : ScreenBase
    {
        GameScreen Parent;
        Rectangle pauseRect = new Rectangle();
        int currentPage = 1;

        public HelpScreen(GameScreen parent)
        {
            Parent = parent;
        }
        public override void Init()
        {
            pauseRect.Width = 300;
            pauseRect.Height = 150;
            Canvas.SetLeft(pauseRect, Game.Instance.CANVAS_WIDTH / 2 - pauseRect.Width / 2);
            Canvas.SetTop(pauseRect, Game.Instance.CANVAS_HEIGHT / 2 - pauseRect.Height / 2);
            Game.Instance.Canvas.Children.Add(pauseRect);

            base.Init();
        }

        public override void Update(GameTime time)
        {
            ProcessKeys();

            Paint();

            //Parent.Update(time);
            base.Update(time);
        }


        private void ProcessKeys()
        {
            if (KeyState.GetNewlyPressedKey(Key.Right) || KeyState.GetNewlyPressedKey(Key.Enter) || KeyState.GetNewlyPressedKey(Key.Space))
                currentPage++;
            if (KeyState.GetNewlyPressedKey(Key.Left))
                currentPage--;

            if (currentPage <= 1) currentPage = 1;
            if (currentPage > 3)
            {
                Game.Instance.SwitchScreen(this.Parent); // resume game
            }

            if (KeyState.GetNewlyPressedKey(Key.Escape))
            {
                Game.Instance.SwitchScreen(this.Parent); // resume game
            }
        }

        private void Paint()
        {
            WriteableBitmap wb = new WriteableBitmap((int)pauseRect.Width, (int)pauseRect.Height);
            wb.Invalidate();
            wb.FillRectangle(0, 0, wb.PixelWidth, wb.PixelHeight, Util.BackgroundGrey);
            int textTop = 15;
            if (currentPage == 1)
            {
                Util.DrawText(wb, "TO ESCAPE YOU MUST ACTIVATE\nALL OF THE SWITCH PLATES", 17, 150, textTop, Colors.White, TextAlignment.Center);
                Util.DrawText(wb, "NEXT ->", 14, 290, 125, Color.FromArgb(255, 150, 150, 150), TextAlignment.Right);
            }
            if (currentPage == 2)
            {
                Util.DrawText(wb, "PRESS ENTER TO UPLOAD YOUR A.I.\nINTO DEACTIVATED ROBOTS", 17, 150, textTop, Colors.White, TextAlignment.Center);
                Util.DrawText(wb, "NEXT ->", 14, 290, 125, Color.FromArgb(255, 150, 150, 150), TextAlignment.Right);
                Util.DrawText(wb, "<- PREV", 14, 10, 125, Color.FromArgb(255, 150, 150, 150), TextAlignment.Left);
            }
            if (currentPage == 3)
            {
                Util.DrawText(wb, "ARROWS OR WASD", 17, 10, textTop, Colors.White, TextAlignment.Left);
                Util.DrawText(wb, "MOVE", 17, 290, textTop, Colors.Yellow, TextAlignment.Right);

                textTop += 25;
                Util.DrawText(wb, "ENTER OR SPACE", 17, 10, textTop, Colors.White, TextAlignment.Left);
                Util.DrawText(wb, "UPLOAD A.I.", 17, 290, textTop, Colors.Yellow, TextAlignment.Right);

                textTop += 25;
                Util.DrawText(wb, "ESC", 17, 10, textTop, Colors.White, TextAlignment.Left);
                Util.DrawText(wb, "PAUSE / HELP", 17, 290, textTop, Colors.Yellow, TextAlignment.Right);

                textTop += 25;
                Util.DrawText(wb, "PG UP/DN", 17, 10, textTop, Colors.White, TextAlignment.Left);
                Util.DrawText(wb, "CHEAT", 17, 290, textTop, Colors.Yellow, TextAlignment.Right);

                Util.DrawText(wb, "FINISH ->", 14, 290, 125, Color.FromArgb(255, 150, 150, 150), TextAlignment.Right);
                Util.DrawText(wb, "<- PREV", 14, 10, 125, Color.FromArgb(255, 150, 150, 150), TextAlignment.Left);
            }
            wb.Invalidate();

            var imageBrush = new ImageBrush
            {
                Stretch = Stretch.None,
                AlignmentX = AlignmentX.Left,
                AlignmentY = AlignmentY.Top
            };
            imageBrush.ImageSource = wb;
            this.pauseRect.Fill = imageBrush;
        }

        public override void Remove()
        {
            if (Game.Instance.Canvas.Children.Contains(pauseRect))
                Game.Instance.Canvas.Children.Remove(pauseRect);
            base.Remove();
        }
    }
}
